package com.cms.manage.util;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.apache.commons.codec.digest.DigestUtils;

import com.cms.manage.common.ConmonConfig;
import com.sun.mail.util.MailSSLSocketFactory;
/**
 * @Description: 发送邮件工具类
 * @author: sgy
 */
public class SendEmailUtil {
	 /**
     * @Title: sendEmail
     * 
     * @Description: 发送邮件工具类方法
     * 
     * @param sendEmail
     *            发件人地址
     * @param sendEmailPwd
     * @param title
     *            邮件标题
     * @param content
     *            邮件内容
     * @param toEmilAddress
     *            收件人地址
     * @throws Exception
     * 
     * @return: void
     */
    public static void sendEmail(String title, String content,String[] toEmilAddress) throws Exception {
        Properties props = new Properties();
        // 开启debug调试，以便在控制台查看
        props.setProperty("mail.debug", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", ConmonConfig.getEmailHost());
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");
        // 开启SSL加密，否则会失败
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);
        Session session = Session.getInstance(props);
        Message msg = new MimeMessage(session);
        // 发送的邮箱地址
        msg.setFrom(new InternetAddress(ConmonConfig.getEmailSend()));
        // 设置标题
        msg.setSubject(title);
        // 设置内容
        msg.setContent(content, "text/html;charset=gbk;");
        Transport transport = session.getTransport();
        //设置账号密码
        transport.connect(ConmonConfig.getEmailHost(),Integer.parseInt(ConmonConfig.getEmailPort()),
        		   ConmonConfig.getEmailName(),ConmonConfig.getEmailPassword());
        // 发送到的邮箱地址
        transport.sendMessage(msg, getAddress(toEmilAddress));

        if(transport!=null){
             try {
                 transport.close();
             } catch (MessagingException e) {
                 e.printStackTrace();
             }
         }
    }
     /**
     * 
     * @Title: getAddress
     * @Description: 遍历收件人信息
     * @param emilAddress
     * @return
     * @throws Exception
     * @return: Address[]
     */
    private static Address[] getAddress(String[] emilAddress) throws Exception {
        Address[] address = new Address[emilAddress.length];
        for (int i = 0; i < address.length; i++) {
            address[i] = new InternetAddress(emilAddress[i]);
        }
        return address;
    }

    public static void main(String[] args) throws Exception {
    	
    	//验证码
		String code =DigestUtils.md5Hex("1360215811@qq.com"+new Date().getTime());
	    //邮件内容
		String content="<div>"+
				"Dear member "+"1360215811@qq.com"+" :<br />"+
				"Thank you for using our website. Please click the link below to reset your password. If your mail does not show the link, you can copy the following link and"+
				"enter it in the browser to reset your password.<br /><br />"+
				"<a href="+ConmonConfig.getSiteDomain()+"modules.php?app=user_pw_reset&uid="+"1360215811@qq.com"+"&email="+"1360215811@qq.com"+"&code="+code+">"+ConmonConfig.getSiteDomain()+"modules.php?app=user_pw_reset&uid="+"1360215811@qq.com"+"&email="+"1360215811@qq.com"+"&code="+code+"</a>"+
				"<br /><br />"+
				"Tips: This link is valid for 24 hours. For your account security, please protect your links and do not let others get your reset password link. We are pleased to help you and thank you for your understanding and support.<br />"+
				"Hope you will satisfy with our total services as well as the product and look forward to your good feedback soon. If you have any questions, please feel free to contact us at anytime.<br />"+
				"<br />"+
				"Regards<br />"+
				"<br />"+
				"Himew Customer Service Team<br />"+
				"Copyright Himew.com  All rights reserved."+
				"</div>";	
		String title="u_forgot_password";
		String [] toEmilAddress={"1360215811@qq.com"};	
        SendEmailUtil.sendEmail(title, content, toEmilAddress);
    }
}


